<script lang="ts" setup>
import { h, ref } from 'vue'
import {
  LineChartOutlined,
  PieChartOutlined,
  HistoryOutlined,
  LogoutOutlined,
  UserOutlined,
  RocketOutlined,
  PictureOutlined,
  FundViewOutlined,
} from '@ant-design/icons-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { message } from 'ant-design-vue'
import { userLogout } from '@/api/userController.ts'
const router = useRouter()

// 菜单列表
const items = [
  {
    key: '/',
    icon: () => h(LineChartOutlined),
    label: '数据总览',
    title: '数据总览',
  },
  {
    key: '/detail',
    icon: () => h(PieChartOutlined),
    label: '数据详情',
    title: '数据详情',
  },
  {
    key: '/picture',
    icon: () => h(PictureOutlined),
    label: '图片详情',
    title: '图片详情',
  },
  {
    key: '/detection',
    icon: () => h(FundViewOutlined),
    label: '黄叶检测',
    title: '黄叶检测',
  },
  {
    key: '/flight-control',
    icon: () => h(RocketOutlined),
    label: '飞控界面',
    title: '飞控界面',
  },
  {
    key: '/chat_history',
    icon: () => h(HistoryOutlined),
    label: '历史记录',
    title: '历史记录',
  },
]
// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}

// 当前选中菜单
const current = ref<string[]>([])
// 监听路由变化，更新当前选中菜单
router.afterEach((to) => {
  current.value = [to.path]
})

const loginUserStore = useLoginUserStore()
// 用户注销
const doLogout = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}
</script>

<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <RouterLink to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.jpg" alt="logo" />
            <div class="title">智慧农业平台</div>
          </div>
        </RouterLink>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-if="loginUserStore.loginUser.userName != '未登录'"
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <ASpace>
                <a-avatar :src="loginUserStore.loginUser.userAvatar">
                  <template #icon>
                    <UserOutlined />
                  </template>
                </a-avatar>
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </ASpace>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <router-link to="/user/info">
                      <UserOutlined />
                      个人信息
                    </router-link>
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
